import uuid from 'uuid';
import insert from './atom/db/insert';
import query from './atom/db/query';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	no: string; menu_no: string; menu_name: string; menu_icon: string; menu_order: number;
}

interface deskScreen extends IBaseTable {
	no: string; user_code: string; user_name: string; screen_no: string;
}

async function atom(msg: Message) {
	const systemid = '6051';
	// 桌面分屏
	const q = query(action_id, session_id, spaceid, systemid);
	q.prepare('desk_screen', ['no', ' user_code', ' user_name', ' screen_no'], {}, 1, 1);
	const [data] = await q.exec();
	if (data.length > 0) {
		const info = data[0] as deskScreen;
		const { exec, prepare } = insert(action_id, session_id, spaceid, systemid);

		// 快捷菜单
		prepare('quick_menu', [{ _id: uuid(), no: uuid(), user_code: info.user_code, user_name: info.user_name, screen_no: info.no, menu_no: msg.menu_no, menu_name: msg.menu_name, menu_icon: msg.menu_icon, menu_order: msg.menu_order }]);
		const res = await exec();

		if (res.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	} else {
		return 'fail';
	}

}